from django.shortcuts import render
from django.views import View
from areas.models import Area
from django import http
from meiduo_mall.utils.response_code import RET

#1, 获取区域
class AreaView(View):
    def get(self,request):

        #0,获取参数
        area_id = request.GET.get("area_id")

        #1,判断获取省,还是市信息
        if not area_id:
            #1, 查询所有的省信息
            provinces = Area.objects.filter(parent=None).all()

            #2, 拼接数据
            province_list = []
            for province in provinces:
                item_dict = {
                    "id":province.id,
                    "name":province.name
                }
                province_list.append(item_dict)

            data_dict = {
                "code":RET.OK,
                "province_list":province_list
            }

            #3, 返回响应
            return http.JsonResponse(data_dict)
        else:
            #1, 根据省的id,获取该省的所有市; 或者根据市的id获取区的信息
            cities = Area.objects.filter(parent_id=area_id).all()

            #2, 拼接数据
            subs = []
            for city in cities:
                city_dict = {
                    "id":city.id,
                    "name":city.name
                }
                subs.append(city_dict)

            data_dict = {
                "code":RET.OK,
                "sub_data":{
                    "subs":subs
                }
            }

            #3, 返回响应
            return http.JsonResponse(data_dict)
